﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace GameNesSL.Views
{
    using System.Windows.Media.Imaging;

    public partial class StartPage : Page
    {
        public StartPage()
        {
            InitializeComponent();
            screen2.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(screen_ImageFailed);
            screen1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(screen_ImageFailed);

        }

        private void GetDescription()
        {
            //description.Visibility = System.Windows.Visibility.Visible;
            //description.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
            //string dummyDescr = "dummy description\r\ndummy description";
            //string[] lines = dummyDescr.Split('\r', '\n');
            //foreach (var line in lines)
            //{
            //    if (!string.IsNullOrEmpty(line))
            //    {
            //        description.Inlines.Add(line);
            //    }
            //    else
            //    {
            //        description.Inlines.Add(new LineBreak());
            //    }
            //}
        }

        private void GetLoadCaps()
        {
            load.IsEnabled = Cache.GameCache.HasSave(GameContext.webMethods.GameID);
        }

        private List<BitmapImage> screens = new List<BitmapImage>();
        private int screenpos = 0;

        private void GetScreenShots()
        {
            GameContext.webMethods.Client.GetScreenshotsUrlCompleted += new EventHandler<GameNesSL.WebRef.GetScreenshotsUrlCompletedEventArgs>(Client_GetScreenshotsUrlCompleted);
            GameContext.webMethods.Client.GetScreenshotsUrlAsync(GameContext.webMethods.GameID);


        }

        void Client_GetScreenshotsUrlCompleted(object sender, GameNesSL.WebRef.GetScreenshotsUrlCompletedEventArgs e)
        {
            try
            {

                List<Uri> urls = new List<Uri>();
                foreach (var result in e.Result)
                {
                    urls.Add(new Uri(result, UriKind.Absolute));
                }

                foreach (var url in urls)
                {
                    screens.Add(new BitmapImage { UriSource = url});
                }
                if (screens.Count > 0)
                {
                    screen2.Source = screens[0];
                }
                if (screens.Count > 1)
                {
                    screen1.Source = screens[1];
                }
            }
            catch (Exception)
            {
                //TODO: no screenshots
            }
        }

        void screen_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            ((Image)sender).Source = new BitmapImage(new Uri("/GameNesSL;Component/images/screens/no_screenshot.png", UriKind.Relative));
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            GameContext.stats.TimeStartPageStart = DateTime.UtcNow;

            if (!string.IsNullOrEmpty(GameContext.webMethods.GameID))
            {
                embeddLink.EmbedID = GameContext.webMethods.GameID;
                load.IsEnabled = false;
                GetLoadCaps();
                GetScreenShots();
                GetDescription();
            }
            else
            {
                GameContext.ErrorDescription = "Bad game id";
                NavigationService.Navigate(new Uri("/Error", UriKind.Relative));
            }

        }



        private void start_Click(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Loading", UriKind.Relative));
        }

        private void load_Click(object sender, MouseButtonEventArgs e)
        {
            GameContext.stats.LoadLastSave = true;
            LoadState();
            NavigationService.Navigate(new Uri("/Loading", UriKind.Relative));
        }

        private void LoadState()
        {
            GameContext.ShouldLoadState = true;
        }

        private void screen1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Scroll left
            if (screens.Count > 0)
            {
                screenpos++;
                screen2.Source = screens[screenpos%screens.Count];
                screen1.Source = screens[(screenpos + 1)%screens.Count];
                fromRigth.Begin();
            }
        }

        private void settings_Click(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings", UriKind.Relative));
        }
    }
}
